// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts')
// 引入柱状图
require('echarts/lib/chart/bar')
require('echarts/lib/chart/line')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')
require('echarts/lib/component/grid')
import { parseTime } from '@/utils/index'
const getLastDays = (nowDate: any, dayStand: number) => {
  let tempDate = new Date(nowDate)
  let tempArrData = []
  for (let i = 0; i < dayStand; i++) {
    tempArrData.unshift(
      parseTime(tempDate.getTime() - i * 24 * 60 * 60 * 1000, '{m}/{d}')
    )
  }

  return tempArrData
}
const getRandomDataPercent = (dayStand: number) => {
  let tempDate = Array(dayStand).fill(100.0)
  for (let i = 0; i <= Math.floor(dayStand / 4); i++) {
    tempDate[Math.floor(Math.random() * dayStand)] = (
      99.8 +
      0.2 * Math.random()
    ).toFixed(2)
  }
  return tempDate
}
const getRandomDataNum = (dayStand: number) => {
  let tempDate = []
  for (let i = 0; i <= dayStand; i++) {
    tempDate.push(Math.ceil(100 * Math.random()))
  }
  return tempDate
}
export default class EchartsConfig {
  private myChart: any
  private option = {
    title: {
      text: '男女比例',
      subtext: '',
      x: 'center'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      data: ['男', '女']
    },
    series: [
      {
        name: '用户统计占比',
        type: 'pie',
        radius: '55%',
        center: ['50%', '60%'],
        data: [
          { value: Math.ceil(1000 * Math.random()), name: '男' },
          { value: Math.ceil(1000 * Math.random()), name: '女' }
        ],
        itemStyle: {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }

  constructor(domId: string) {
    this.myChart = echarts.init(document.getElementById(domId))
  }
  public setOption(config: any) {
    this.option.title.subtext = config.name
    this.myChart && this.myChart.setOption(this.option)
  }
}
